<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Xcode8插件开发"><meta name="keywords" content=""><meta name="author" content="kyren"><meta name="copyright" content="kyren"><title>Xcode8插件开发 | Kyren's Blog</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cf87fcdbd9ab7cf93d894f7c219ae78f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script src="https://v1.hitokoto.cn/?encode=js&amp;charset=utf-8&amp;select=.footer_custom_text" defer></script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '6.2.0'
} </script><meta name="generator" content="Hexo 6.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#Extension%E7%89%B9%E6%80%A7"><span class="toc-number">1.</span> <span class="toc-text">Extension特性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Extension%E7%BC%BA%E9%99%B7"><span class="toc-number">2.</span> <span class="toc-text">Extension缺陷</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-%E6%B5%8B%E8%AF%95Extension"><span class="toc-number">3.</span> <span class="toc-text">7.测试Extension</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-%E7%BB%91%E5%AE%9A%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-number">4.</span> <span class="toc-text">8.绑定快捷键</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://avatars.githubusercontent.com/u/5827823?v=4"></div><div class="author-info__name text-center">kyren</div><div class="author-info__description text-center">huluo的技术博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://github.com/huluo666">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">239</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">41</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">29</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">Links</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:9090/">Jira</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:8090/">Confluence</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://bloc.leniugame.com/">Leniu</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Kyren's Blog</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/essay">随笔</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/about">关于</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">Xcode8插件开发</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2017-06-05</time></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>Xcode8以后, 以前的插件都不能用了。Apple官方推荐的方式<em>Xcode Source Editor Extension</em>来自己制作插件. Extension的方式开发的插件, 可以独立上架AppStore, 并且是独立于Xcode工程独立运行的. 但是没有UI交互, 不能在后台运行并且只能在开发者调用的时候直接修改代码。</p>
<p><img src="https://github.com/huluo666/NetWorkMonitorView/blob/master/NetWorkMonitorView/imge0.gif" alt="https://github.com/huluo666/NetWorkMonitorView/blob/master/NetWorkMonitorView/imge0.gif"></p>
<p>官方文档</p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/documentation/xcodekit">https://developer.apple.com/documentation/xcodekit</a></p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/documentation/xcodekit/creating_a_source_editor_extension">Creating a Source Editor Extension</a></p>
<p>测试扩展</p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/documentation/xcodekit/testing_your_source_editor_extension">Testing Your Source Editor Extension</a></p>
<h4 id="Extension特性"><a href="#Extension特性" class="headerlink" title="Extension特性"></a>Extension特性</h4><ul>
<li>支持上架到AppStore</li>
<li>每个Extension运行在独立的进程，如果它崩溃了，不会引起Xcode的崩溃，且会有错误提示</li>
</ul>
<h4 id="Extension缺陷"><a href="#Extension缺陷" class="headerlink" title="Extension缺陷"></a>Extension缺陷</h4><ul>
<li>无UI交互</li>
<li>只能够在开发者调用相关命令的时候直接的修改代码，具体表现为 <strong>获取正在编辑的文本</strong> <strong>获取选中的区域</strong> <strong>替换正在编辑的文本</strong> <strong>选中正在编辑的文本</strong> <strong>在Editor菜单中生成一个子菜单，用于调用插件</strong> <strong>绑定快捷键</strong></li>
<li>Extension不能在后台运行</li>
</ul>
<p>1、新建工程</p>
<p>打开Xcode-&gt; <code>command + shift + N</code> -&gt;选择macOS -&gt; Cocoa Application, 点击Next新建一个工程。</p>
<p>2、新增Target（Extension）</p>
<p>在Xcode工具栏中选择<code>Editor -&gt; Add Target -&gt; MacOS -&gt; Xcode Source Editor Extension-点击Activate</code></p>
<p>新增后系统会默认创建<code>SourceEditorExtension</code>，<code>SourceEditorCommand</code>两个类。</p>
<p> <code>invocation.commandIdentifier</code>; 命令标识符，对应plist文件中的<code>XCSourceEditorCommandIdentifier</code>字段，一个扩展可以有多个功能，用以区分需要执行的命令事件。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Identifier=com.pcauto.PCDevTools.AutoPropertyPlug.SourceEditorCommand</span><br><span class="line">Identifier=com.pcauto.PCDevTools.AutoPropertyPlug.SourceEditorCommand2</span><br></pre></td></tr></table></figure>



<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="string">&quot;ViewController.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">ViewController</span>()</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) <span class="built_in">NSString</span> *name;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">ViewController</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="type">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="variable language_">super</span> viewDidLoad];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure>



<p>选择name属性行，点击插件生成属性</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="string">&quot;ViewController.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">ViewController</span>()</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) <span class="built_in">NSString</span> *name;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">ViewController</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="type">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="variable language_">super</span> viewDidLoad];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="built_in">NSString</span> *)name &#123;</span><br><span class="line">    <span class="keyword">if</span> (!_name) &#123;</span><br><span class="line">        _name = [[<span class="built_in">NSString</span> alloc] init];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> _name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure>



<p><code>invocation.buffer.lines</code>是一个可变字符串数组，由于属性代码是倒序的，所以将属性代码依次插入到固定行即可，如果属性代码是顺序的，那么插入位置行数要++1</p>
<p>将属性代码插入到文本末尾</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="type">void</span>)insertCodeWithStrings:(<span class="built_in">NSArray</span> *)getterStrings &#123;</span><br><span class="line">      <span class="built_in">NSLog</span>(<span class="string">@&quot;getterStrings=%@&quot;</span>,getterStrings);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; getterStringArray.count; i++) &#123;</span><br><span class="line">        <span class="built_in">NSLog</span>(<span class="string">@&quot;ViewController=%@-%zd&quot;</span>,<span class="keyword">self</span>.invocation.buffer.lines,<span class="keyword">self</span>.endLineNumber);</span><br><span class="line">        [<span class="keyword">self</span>.invocation.buffer.lines insertObject:getterStrings[i] atIndex:<span class="keyword">self</span>.endLineNumber];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>invocation.buffer.lines 所有行</p>
<h4 id="7-测试Extension"><a href="#7-测试Extension" class="headerlink" title="7.测试Extension"></a>7.测试Extension</h4><p>点击xcode工具栏-&gt;Editor-&gt;插件菜单</p>
<h4 id="8-绑定快捷键"><a href="#8-绑定快捷键" class="headerlink" title="8.绑定快捷键"></a>8.绑定快捷键</h4><ol>
<li>添加快捷键: Xcode -&gt; “Preferences” -&gt; “Key Bindings” -&gt; 搜索插件名字 -&gt; 添加对应的快捷键.</li>
</ol>
<p>快捷键<code>command+，</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">占位符的格式：&lt;#param#&gt; &lt;#className#&gt; &lt;#objName#&gt;</span><br><span class="line"></span><br><span class="line">- (#className# *)#objName# &#123;\n    if (!_#objName#) &#123;\n        _#objName# = [[#className# alloc] init];\n    &#125;\n    return _#objName#;\n&#125;\n</span><br></pre></td></tr></table></figure>



<p>系统偏好设置路径</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">/System/Library/PreferencePanes</span><br><span class="line">/System/Library/PreferencePanes/Extensions.prefPane 扩展</span><br></pre></td></tr></table></figure>

<p>问题备忘与解决方法</p>
<p>1、<code>There is a problem launching using posix_spawn (error code: 2).</code></p>
<p>将Xcode加入到Scheme的执行菜单里</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">打开Xcode-&gt;Product-&gt;EditScheme-&gt;Run-&gt;Executable-&gt;Xcode.app</span><br></pre></td></tr></table></figure>

<p><img src="http://7xr7vj.com1.z0.glb.clouddn.com/UC20180306_092216.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">IDEExtensionManager: Xcode Extension does not meet code signing requirement: Error Domain=DVTSecErrorDomain Code=-67050 &quot;code failed to satisfy specified code requirement(s)&quot; UserInfo=&#123;NSLocalizedDescription=code failed to satisfy specified code requirement(s)&#125;</span><br></pre></td></tr></table></figure>

<p>工程Target和Xcode Extension Target 需要选择一个签名证书 <code>Target-&gt;signing-&gt;team-&gt;签名证书</code></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">kyren</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://huluo666.gitee.io/2017/06/05/Xcode8插件开发/">http://huluo666.gitee.io/2017/06/05/Xcode8插件开发/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://huluo666.gitee.io">Kyren's Blog</a>！</span></div></div><div class="post-meta__tag-list"></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2017/06/06/MAC%E4%B9%8Bhttp%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%B7%A5%E5%85%B7/"><i class="fa fa-chevron-left">  </i><span>MAC之http命令行工具curl</span></a></div><div class="next-post pull-right"><a href="/2017/05/08/Mac%E7%BB%88%E7%AB%AF%E5%BF%AB%E6%8D%B7%E9%94%AE/"><span>Mac iterm2快捷键</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2022 By kyren</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">hitokoto</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/lib/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>